package com.itentbase.match.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

/**
 * @author:wjl
 * @create: 2023-09-18 15:59
 * @Description:
 */
@Slf4j
public class RequestUtils {
    private static final String ENCRYPT_SALT_BY_CUSTOM="isDAmowang";
    public static final String LIVE_MATCH="live_match";
    /**
     * 获取请求的IP地址
     * @param request
     * @return {@link String }
     * @author wjl
     * @date 2023-09-18
     **/
    public static String getIPAddress(HttpServletRequest request){
        String ipAddress = request.getHeader("x-forwarded-for");
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
        }
        if ("0:0:0:0:0:0:0:1".equals(ipAddress)) {
            ipAddress = "127.0.0.1";
        }
        return ipAddress;
    }


    public static String encryptByCustom(LocalDate src1, String src2){
        return DigestUtils.md5Hex(DigestUtils.md5Hex(src1+src2)+ENCRYPT_SALT_BY_CUSTOM);
    }


}
